package com.nutricia.biz.impl;

import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import com.nutricia.biz.convertor.SystemRoleConvertor;
import com.nutricia.common.api.SystemRoleFacade;
import com.nutricia.common.api.request.CreateSystemRoleReq;
import com.nutricia.common.api.request.DeleteSystemRoleReq;
import com.nutricia.common.api.request.SaveRolePermissionReq;
import com.nutricia.common.api.request.UpdateSystemRoleReq;
import com.nutricia.common.api.request.VoidRequest;
import com.nutricia.common.api.result.CreateSystemRoleRes;
import com.nutricia.common.api.result.GetSystemRolesRes;
import com.nutricia.common.api.result.VoidResult;
import com.nutricia.common.lang.ServiceCallBack;
import com.nutricia.common.util.CheckUtil;
import com.nutricia.core.ex.BizErrorCode;
import com.nutricia.core.model.domain.SystemRoleDO;

/**
 * 系统角色
 *
 * @author: huangsheng
 * @date: 2017/10/24
 */
@Service("systemRoleFacade")
public class SystemRoleFacadeImpl extends AbstractFacadeImpl implements SystemRoleFacade {

    /**
     * 获取系统角色数据
     *
     * @author: huangsheng
     * @date: 2017/10/24 上午10:38
     */
    public GetSystemRolesRes getSystemRoles(@RequestBody VoidRequest voidRequest) {
        logger.info("获取系统角色数据--getSystemRoles");
        final GetSystemRolesRes result = new GetSystemRolesRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {

            }

            @Override
            public void doService() throws Exception {
                result.setRoles(SystemRoleConvertor.convertToSystemRoleInfos(systemRoleService.getSystemRoles()));
                result.setSuccess(true);
            }

        });
    }

    /**
     * 新增系统角色数据
     *
     * @author: huangsheng
     * @date: 2017/10/24 上午10:38
     */
    public CreateSystemRoleRes createSystemRole(@RequestBody CreateSystemRoleReq createSystemRoleReq) {
        logger.info("新增系统角色数据--createSystemRole");
        final CreateSystemRoleRes result = new CreateSystemRoleRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                CheckUtil.isNotBlank(createSystemRoleReq.getRoleName(),"roleName为空");
                CheckUtil.isNotBlank(createSystemRoleReq.getRoleCode(), "roleCode为空");
            }

            @Override
            public void doService() throws Exception {

                SystemRoleDO systemRoleDO = new SystemRoleDO();
                systemRoleDO.setRoleName(createSystemRoleReq.getRoleName());
                systemRoleDO.setRoleCode(createSystemRoleReq.getRoleCode());

                String roleId = systemRoleService.createSystemRole(systemRoleDO);
                logger.info("成功新增 ID= {} 的角色数据", roleId);
                result.setRoleId(roleId);
                result.setSuccess(true);
            }

        });
    }

    /**
     * 更新系统角色数据
     *
     * @author: huangsheng
     * @date: 2017/10/24 上午10:38
     */
    public VoidResult updateSystemRole(@RequestBody UpdateSystemRoleReq updateSystemRoleReq) {
        logger.info("更新系统角色数据--updateSystemRole");
        final VoidResult result = new VoidResult();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                CheckUtil.isNotBlank(updateSystemRoleReq.getRoleId(), "roleId为空");
                CheckUtil.isNotBlank(updateSystemRoleReq.getRoleName(), "roleName为空");
                CheckUtil.isNotBlank(updateSystemRoleReq.getRoleCode(), "roleCode为空");
            }

            @Override
            public void doService() throws Exception {

                logger.info("更新：{} 的角色数据", updateSystemRoleReq.getRoleId());
                SystemRoleDO systemRoleDO = new SystemRoleDO();
                systemRoleDO.setId(updateSystemRoleReq.getRoleId());
                systemRoleDO.setRoleName(updateSystemRoleReq.getRoleName());
                systemRoleDO.setRoleCode(updateSystemRoleReq.getRoleCode());

                systemRoleService.updateSystemRole(systemRoleDO);
                result.setSuccess(true);
            }

        });
    }

    /**
     * 删除系统角色数据（逻辑删除）
     *
     * @author: huangsheng
     * @date: 2017/10/24 上午10:38
     */
    public VoidResult deleteSystemRoe(@RequestBody DeleteSystemRoleReq deleteSystemRoleReq) {
        logger.info("删除系统角色数据--deleteSystemRoe");
        final VoidResult result = new VoidResult();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                CheckUtil.isNotBlank(deleteSystemRoleReq.getRoleId(), "roleId为空");
            }

            @Override
            public void doService() throws Exception {
                logger.info("删除：{} 的角色数据", deleteSystemRoleReq.getRoleId());

                systemRoleService.deleteSystemRole(deleteSystemRoleReq.getRoleId());
                result.setSuccess(true);
            }

        });
    }

    /**
     * 保存角色的权限数据
     *
     * @author: huangsheng
     * @date: 2017/10/26 上午11:57
     */
    public VoidResult saveRolePermission(@RequestBody SaveRolePermissionReq saveRolePermissionReq) {
        logger.info("保存角色的权限数据--saveRolePermission");
        final VoidResult result = new VoidResult();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                CheckUtil.isNotBlank(saveRolePermissionReq.getRoleId(), "roleId为空");
                CheckUtil.isTrue(null != saveRolePermissionReq.getPermissionIds(),BizErrorCode.PARAM_EMPTY, "permissionIds为空");
            }

            @Override
            public void doService() throws Exception {
                logger.info("保存角色ID= {}, 权限数量 ={}", saveRolePermissionReq.getRoleId(), saveRolePermissionReq.getPermissionIds().size());
                systemRoleService.saveRolePermission(saveRolePermissionReq.getRoleId(), saveRolePermissionReq.getPermissionIds());
                result.setSuccess(true);
            }

        });
    }

}
